import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:high_help_apps/common/values/colors.dart';
import 'package:high_help_apps/common/widgets/common/common_ink_well.dart';

import 'second_hand_market_logic.dart';
import 'second_hand_market_state.dart';

class SecondHandMarketPage extends StatefulWidget {
  @override
  _SecondHandMarketPageState createState() => _SecondHandMarketPageState();
}

class _SecondHandMarketPageState extends State<SecondHandMarketPage>
    with AutomaticKeepAliveClientMixin {
  final SecondHandMarketLogic logic = Get.put(SecondHandMarketLogic());
  final SecondHandMarketState state = Get.find<SecondHandMarketLogic>().state;

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
      child: GetBuilder<SecondHandMarketLogic>(
        builder: (logic) => logic.isInit == false
            ? Center(
                child: CircularProgressIndicator(
                  color: AppColors.primaryElement,
                ),
              )
            : ListView.builder(
                itemCount: logic.state.commodityInfoEntity.records.length,
                itemBuilder: (BuildContext context, int pIndex) {
                  return Card(
                    margin: EdgeInsets.all(10.r),
                    child: CommonInkWell(
                      child: Column(
                        children: <Widget>[
                          Container(
                            child: AspectRatio(
                              aspectRatio: 20 / 15,
                              child: GetBuilder<SecondHandMarketLogic>(
                                builder: (logic) => Swiper(
                                  itemBuilder:
                                      (BuildContext context, int index) {
                                    return new Image.network(
                                      "http://" +
                                          logic
                                              .state
                                              .commodityInfoEntity
                                              .records[pIndex]
                                              .commodityImages[index]
                                              .token,
                                      fit: BoxFit.cover,
                                    );
                                  },
                                  itemCount: logic.state.commodityInfoEntity
                                      .records[pIndex].commodityImages.length,
                                  pagination: SwiperPagination(),
                                  scale: 0.9,
                                ),
                              ),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.all(12.r),
                            child: ListTile(
                              leading: ClipOval(
                                child: Image.network(
                                    "http://" +
                                        logic.state.commodityInfoEntity
                                            .records[pIndex].user.avatar,
                                    fit: BoxFit.cover,
                                    height: 45.r,
                                    width: 45.r),
                              ),
                              title: Text(
                                logic.state.commodityInfoEntity.records[pIndex]
                                    .title,
                                overflow: TextOverflow.ellipsis,
                              ),
                              subtitle: Text(logic.state.commodityInfoEntity
                                  .records[pIndex].desc),
                            ),
                          )
                        ],
                      ),
                      onTap: () => logic.redirectIntoCommodityInfo(pIndex),
                    ),
                  );
                }),
      ),
      onRefresh: () => logic.loadCommodityInfo(),
      color: AppColors.primaryElement,
    );
  }

  @override
  void dispose() {
    Get.delete<SecondHandMarketLogic>();
    super.dispose();
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
